package com.luo.ssm.controller;

import com.luo.ssm.utils.AuthCodeUtil;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author : luocheng
 * @date : Created in 2021/8/28 16:09
 * @description : 用于生成验证码的控制器
 */
@Controller
public class AuthCodeController {
    @RequestMapping("/autoCode")
    @ResponseStatus(HttpStatus.OK)
    public void getAutoCode(HttpSession session, HttpServletResponse response){
        String code= AuthCodeUtil.getCode();
        //把验证码code放入session中
        session.setAttribute("code",code);
        //使用验证码工具类生成一张图片
        BufferedImage authCodeImg = AuthCodeUtil.getAuthCodeImg(code);
        //把改图响应给客户端
        try {
            ImageIO.write(authCodeImg,"JPEG",response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
